﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using SunTzu.Core.Data;

namespace Odyssey.Web.Areas.Admin.Controllers
{
    public class Left_navigationController : BaseController<tb_left_navigation>
    {

        private int? ClassId
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["classid"]))
                {
                    return null;
                }
                int classId;
                if (int.TryParse(Request.QueryString["classid"], out classId))
                {
                    return classId;
                }
                return null;
            }
        }

        protected override void BeforeSaveOrUpdate(tb_left_navigation entity)
        {
            entity.classid = ClassId.Value;
            base.BeforeSaveOrUpdate(entity);
        }

        public override ActionResult List(int? page)
        {
            if (ClassId.HasValue)
            {
                ViewBag.LeftNavigations = table.Where(it => it.classid == ClassId.Value && it.parent == 0).OrderByDescending(c => c.Id);
                ViewBag.ChildLeftNavigations = table.Where(it => it.classid == ClassId.Value && it.parent != 0).OrderByDescending(c => c.Id).ToList();
                return View();
            }
            return base.List(page);
        }

        public override ActionResult Edit(int? id)
        {
            List<tb_left_navigation> topNav = db.GetTable<tb_left_navigation>().Where(nav => nav.parent.Value == 0 && nav.classid == ClassId.Value).ToList();
            ViewData.Add("TopNav", topNav);
            return base.Edit(id);
        }
    }
}

